﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;

namespace FormsControlLibraryForWindowsAndWeb
{
    [ComVisible(true)]
    public partial class UserControlForLogin : UserControl
    {
        public static int userId;
        public UserControlForLogin()
        {
            InitializeComponent();
        }

        private void button_Logon_Click(object sender, EventArgs e)
        {
            if (GetHTTP.GetWebContent("http://localhost:50530/login/loginName=" + textBox_Login.Text + "_password=" + textBox_Password.Text + "_type=Client").Contains("Client"))
            {
                string test = GetHTTP.GetWebContent("http://localhost:50530/login/loginName=" + textBox_Login.Text + "_password=" + textBox_Password.Text + "_type=Client");
                string[] matches = this.GetMatches<string>(test, "\\d+");
                userId = Convert.ToInt16(ConvertStringArrayToString(matches));
                userControlForClient1.Visible = true;


            }
            else if (GetHTTP.GetWebContent("http://localhost:50530/login/loginName=" + textBox_Login.Text + "_password=" + textBox_Password.Text + "_type=Admin").Contains("Admin"))
            {
                string test = GetHTTP.GetWebContent("http://localhost:50530/login/loginName=" + textBox_Login.Text + "_password=" + textBox_Password.Text + "_type=Admin");
                string[] matches = this.GetMatches<string>(test, "\\d+");
                userId = Convert.ToInt16(ConvertStringArrayToString(matches));
                userControlForAdmin1.Visible = true;


            }
            else
            {
                MessageBox.Show("Invalid login name or password");
            }
        }

        public T[] GetMatches<T>(string Input, string MatchPattern) where T : IConvertible
        {
            List<T> MatchedValues = new List<T>();
            Regex MatchInt = new Regex(MatchPattern);

            MatchCollection Matches = MatchInt.Matches(Input);
            foreach (Match m in Matches)
                MatchedValues.Add((T)Convert.ChangeType(m.Value, typeof(T)));

            return MatchedValues.ToArray<T>();
        }
        static string ConvertStringArrayToString(string[] array)
        {
            //
            // Concatenate all the elements into a StringBuilder.
            //
            StringBuilder builder = new StringBuilder();
            foreach (string value in array)
            {
                builder.Append(value);
            }
            return builder.ToString();
        }
    }
    public abstract class GetHTTP
    {
        static System.IO.Stream st = null;
        static System.IO.StreamReader sr = null;
        public static string GetWebContent(string url)
        {
            System.Net.WebRequest req = System.Net.WebRequest.Create(url);
            // get the response and read from the result stream

            System.Net.WebResponse resp = req.GetResponse();

            st = resp.GetResponseStream();
            sr = new System.IO.StreamReader(st);

            // read all the text in it

            return sr.ReadToEnd();
        }

    }
}
